<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="../utils/js/old_version/react.development.js"></script>
    <script src="../utils/js/old_version/react-dom.development.js"></script>
    <script src="../utils/js/old_version/babel.min.js"></script>
    <script src="../utils/js/old_version/prop-types.js"></script>
    <script type="text/babel">
      class Login extends React.Component {
        state = {
          username: "",
          password: "",
        };
        submit = (e) => {
          e.preventDefault();
          const { username, password } = this.state;
          alert(`姓名：${username},密码：${password}`);
        };

        // 高阶函数的写法 返回一个函数
        // saveData = (type) => {
        //   console.log(type);
        //   return (e) => {
        //     this.setState({
        //       [type]: e.currentTarget.value,
        //     });
        //   };
        // };
        // render() {
        //   return (
        //     <form onSubmit={this.submit}>
        //       姓名：
        //       <input name="username" type="name" onChange={this.saveData("username")} />
        //       密码：
        //       <input name="password" type="password" onChange={this.saveData("password")} />
        //       <button>提交</button>
        //     </form>
        //   );
        // }

        // 不使用高阶函数

        saveData = (type, e) => {
          this.setState({
            [type]: e.currentTarget.value,
          });
        };

        render() {
          return (
            <form onSubmit={this.submit}>
              姓名：
              <input
                name="username"
                type="name"
                onChange={(e) => this.saveData("username", e)}
              />
              密码：
              <input
                name="password"
                type="password"
                onChange={(e) => this.saveData("password", e)}
              />
              <button>提交</button>
            </form>
          );
        }
      }

      ReactDOM.render(<Login />, document.getElementById("app"));
    </script>
  </body>
</html>
